<?php
class AdMenuAction extends AdMenuAbstract {

	public function doMenulist(){
		$menu = self::getOrderMenu();

		$parentData = null;
		if(! empty($menu)){
			// 存在父级
			if(key_exists(0, $menu) && ! empty($menu[0])){
				$parentData = $menu[0];
			}
		}
		$this->smarty->assign('parentmenu', $parentData);
		$this->smarty->assign('menu', $menu);
		$this->smarty->display('admin/menu_list.html');
	}

	/**
	 * 今日网游首页
	 *
	 * @access public
	 * @return void | html
	 * @link
	 *
	 */
	public function doAddpage(){
		$mid = AppCommon::getRequestString("menuid");
		
		$pageType = 1;
		// 如果是修改
		if(AppCommon::intParse($mid, $menuid) && $menuid > 0){
			$menuModule = new AdminModule();
			$menuInfo = $menuModule->getMenuById($menuid);
			
			if(empty($menuInfo)){
				$this->smarty->assign('formaction', "?g=menu&r=adMenu/addmenu");
				$this->smarty->assign('menuid', '0');
				$this->smarty->assign('type', '');
				$this->smarty->assign('action_key', '');
				$this->smarty->assign('filepath', '');
				$this->smarty->assign('name', '');
				$this->smarty->assign('showchecked', "checked='checked'");
				$this->smarty->assign('noshowchecked', '');
				$nowValue = '';
			}else{
				$pageType = 2;
				$this->smarty->assign('menuid', $menuid);
				$this->smarty->assign('type', $menuInfo['type']);
				$this->smarty->assign('action_key', $menuInfo['action_key']);
				$this->smarty->assign('formaction', "?g=menu&r=adMenu/editmenu");
				$this->smarty->assign('filepath', $menuInfo['action']);
				$this->smarty->assign('name', $menuInfo['name']);
				
				if($menuInfo['isshow'] == 'Y'){
					$this->smarty->assign('showchecked', "checked='checked'");
					$this->smarty->assign('noshowchecked', "");
				}else if($menuInfo['isshow'] == 'N'){
					$this->smarty->assign('showchecked', "");
					$this->smarty->assign('noshowchecked', "checked='checked'");
				}
				
				$nowValue = $menuInfo['parentid'];
			}
		}else{
			$this->smarty->assign('formaction', "?g=menu&r=adMenu/addmenu");
			$this->smarty->assign('menuid', '0');
			$this->smarty->assign('type', '');
			$this->smarty->assign('action_key', '');
			$this->smarty->assign('filepath', '');
			$this->smarty->assign('name', '');
			$this->smarty->assign('showchecked', "checked='checked'");
			$this->smarty->assign('noshowchecked', '');
			$nowValue = '';
		}
		
		$menuSelectStr = self::makeAllMenuSelect($nowValue); // $menuModule->makeAllMenuSelect($nowValue);
		
		$this->smarty->assign('pagetype', $pageType);
		$this->smarty->assign('menuSelectStr', $menuSelectStr);
		$this->smarty->display('admin/add_menu.html');
	}
	
	/* ================================== 接口层START ================================ */
	
	/**
	 * 添加菜单接口
	 */
	public static function doAddmenu(){
		$type = AppCommon::getRequestInt("type", 2);
		$name = AppCommon::getRequestString("name", 2);
		$action_key = AppCommon::getRequestString("action_key", 2);
		$url = AppCommon::getRequestString("filepath", 2);
		$pid = AppCommon::getRequestString("parentid", 2);
		$isshow = AppCommon::getRequestString("isshow", 2);
		
		if(empty($name)){
			PageFormat::send(false, '菜单名称不可为空!', "?g=menu&r=adMenu/addpage");
		}
		
		if(empty($isshow) || ! AppCommon::intParse($pid, $parentid)){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adMenu/addpage");
		}
		
		$menuModule = new AdminModule();
		$rs = $menuModule->addMenu($type, $name, $action_key, $url, $parentid, $isshow);
		
		if(!$rs){
			PageFormat::send(false, '操作失败', "?g=menu&r=adMenu/addpage");
		}
		
		PageFormat::send(false, '操作成功', "?g=menu&r=adMenu/menulist");
	}

	/**
	 * 修改菜单接口
	 */
	public static function doEditmenu(){
		$mid = AppCommon::getRequestString("menuid", 2);
		$type = AppCommon::getRequestInt("type", 2);
		$name = AppCommon::getRequestString("name", 2);
		$action_key = AppCommon::getRequestString("action_key", 2);
		$url = AppCommon::getRequestString("filepath", 2);
		$pid = AppCommon::getRequestString("parentid", 2);
		$isshow = AppCommon::getRequestString("isshow", 2);
		
		if(! AppCommon::intParse($mid, $menuid) || $menuid <= 0){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adMenu/addpage");
		}
		
		if(empty($name)){
			PageFormat::send(false, '菜单名称不可为空!', "?g=menu&r=adMenu/addpage");
		}
		
		if(empty($isshow) || ! AppCommon::intParse($pid, $parentid)){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adMenu/addpage");
		}
		
		$menuModule = new AdminModule();
		$menuInfo = $menuModule->getMenuById($menuid);
		
		if(empty($menuInfo)){
			PageFormat::send(false, '非法请求2!', "?g=menu&r=adMenu/addpage");
		}
		
		$rs = $menuModule->editMenu($menuid, $type, $name, $action_key, $url, $parentid, $isshow);
		
		if(! $rs){
			PageFormat::send(false, '添加失败', "?g=menu&r=adMenu/addpage");
		}
		
		PageFormat::send(false, '添加成功', "?g=menu&r=adMenu/menulist");
	}
}
